import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'
Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path:"/",
    name:"portal",
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: () => import("@/views/Home/home.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/game',
    name: 'game',
    component: () => import("@/views/Game/game.vue"),
     meta:{
      requireAuth:true
    }
  },
  {
    path: '/community',
    name: 'community',
    component: () => import("@/views/Community/community.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/myself',
    name: 'myself',
    component: () => import("@/views/Myself/myself.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("@/views/Login/login.vue")
  },
  {
    path: '/resiger',
    name: 'resiger',
    component: () => import("@/views/Regiser/regiser.vue")
  },
  {
        path: '/countingSticks',
        name: 'countingSticks',
        component: () => import("@/views/FirstGrade/countingSticks.vue"),
        meta:{
          requireAuth:true
        }
  },
  {
    path: '/compareCount',
    name: 'compareCount',
    component: () => import("@/views/FirstGrade/compareCount.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/computedFirst',
    name: 'computedFirst',
    component:()=> import("@/views/FirstGrade/computedFirst.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/foudLow',
    name: 'foudLow',
    component:()=> import("@/views/FirstGrade/foudLow.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/unitConversion',
    name: 'unitConversion',
    component:()=> import("@/views/SeconGrade/unitConversion.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/timeColck',
    name: 'timeColck',
    component:()=> import("@/views/SeconGrade/timeColck.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/changeNumber',
    name: 'changeNumber',
    component:()=> import("@/views/SeconGrade/changeNumber.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/fangcheng',
    name: 'fangcheng',
    component:()=> import("@/views/SeconGrade/fangcheng.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/TicTacToe',
    name: 'TicTacToe',
    component:()=> import("@/views/Amusement/TicTacToe.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/fanpaizi',
    name: 'fanpaizi',
    component:()=> import("@/views/Amusement/fanpaizi.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/shudu',
    name: 'shudu',
    component:()=> import("@/views/Amusement/shudu.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/numberBoom',
    name: 'numberBoom',
    component:()=> import("@/views/Amusement/numberBoom.vue"),
    meta:{
      requireAuth:true
    }
  },{
    path: '/colorJudgment',
    name: 'colorJudgment',
    component:()=> import("@/views/Amusement/colorJudgment.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/character',
    name: 'character',
    component:()=> import("@/views/Amusement/character.vue"),
    meta:{
      requireAuth:true
    }
  },
  {
    path: '/share',
    name: 'share',
    component:()=> import("@/views/Myself/share"),
    meta:{
      requireAuth:true
    }
  },
]

const router = new VueRouter({
  mode: 'history',
  routes
})

export default router

//登录拦截
router.beforeEach((to,from,next)=>{
  if(to.meta.requireAuth){
    //若果被拦截
    if(sessionStorage.token){
      next();
    }
    else{
      Message.error('请先登录')
      next({
        path:"/login"
      })
      
    }
  }
  else{
    next();
  }
})